<?php

namespace Modules\DataAnalysis\Entities;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Cache;

class Comment extends Model
{
    use HasFactory;

    /**
     * 不可以批量赋值的属性
     *
     * @var array
     */
    protected $guarded = [];

    protected $appends = ['feeling'];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    /**
     * 文章类型
     *
     * @param string $value
     *
     * @return array
     */
    public function getFeelingAttribute($value)
    {
        if (Cache::get('score_line')) {
            $score_line = Cache::get('score_line');
        } else {
            // 系统设置
            $settings = Setting::query()->get();
            $score_line = $settings->where('key', 'score_line')->first()->value;
            // 将分数线缓存一下【模型中使用】
            Cache::put('score_line', $score_line);
        }
        if ($this->score >= $score_line) {
            return 'positive';
        } else {
            return 'passive';
        }
    }

    /**
     * 说明：反向关联的视频
     *
     * @return
     * @param   type
     * @author  daixinguo (daixinguo57@163.com)
     */
    public function video()
    {
        return $this->belongsTo(Video::class, 'video_id', 'id');
    }

    /**
     * 说明：关联评论分词
     *
     * @return
     * @param   type
     * @author  daixinguo (daixinguo57@163.com)
     */
    public function phrases()
    {
        return $this->hasMany(CommentPhrase::class, 'comment_id', 'id');
    }
}
